package pe.edu.upc.dew.pharma.dao.mysql;

import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

import pe.edu.upc.dew.pharma.dao.ProveedorDao;
import pe.edu.upc.dew.pharma.dao.mapper.ProveedorMapper;
import pe.edu.upc.dew.pharma.model.Proveedor;
import pe.edu.upc.dew.pharma.util.StoredProcedurePharma;

@Repository
public class MysqlProveedorDao extends SimpleJdbcDaoSupport implements ProveedorDao {

	private static final Log logger = LogFactory.getLog(MysqlProveedorDao.class);
	
	@Autowired
	public MysqlProveedorDao(DataSource dataSource){
		setDataSource(dataSource);
	}
	
	@SuppressWarnings("unchecked")
	public List<Proveedor> obtenerProveedores(int idProveedor) {
		
		if(logger.isInfoEnabled()){
			logger.info("obtenerProveedores : idProveedor = " + idProveedor);
		}
		
		List<Proveedor> proveedores = new ArrayList<Proveedor>();		
		StoredProcedurePharma sp = new StoredProcedurePharma(getJdbcTemplate(), "sp_obtener_proveedores");
		if(idProveedor > 0){
			sp.addInParameter("p_proveedor", Types.INTEGER, idProveedor);
		}else{
			sp.addInParameter("p_proveedor", Types.NULL, null);
		}
		Map<String, Object> result = sp.executeProcedure("proveedores", new ProveedorMapper());
		proveedores = (List<Proveedor>) result.get("proveedores");
		
		if(logger.isInfoEnabled()){
			logger.info("resultado = " + proveedores.size());
		}
		
		return proveedores;
	}

}
